package number_operatation.leetcode.easy;

/**
 * @author bruin_du
 * @description 前 n 个数字二进制中 1 的个数
 * @date 2022/8/1 19:18
 **/
public class OfferII003_CountBits {
    public int[] countBits(int n) {
        int[] res = new int[n + 1];
        for (int i = 0; i <= n; i++) {
            res[i] = count(i);
        }
        return res;
    }

    private int count(int n) {
        int num = 0;
        while (n != 0) {
            n &= (n - 1);
            num++;
        }
        return num;
    }
}
